package edu.scau.cxq.gp.service.impl;


import java.io.IOException;
import java.io.InputStream;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import edu.scau.cxq.gp.service.SSHService;
import edu.scau.cxq.gp.ssh.entity.SSHEntity;
import org.springframework.stereotype.Service;

@Service
public class SSHServiceImpl implements SSHService {
    private Connection connect;

    /**
     * 连接服务器并 执行shell脚本
     *
     * @param sshEntity ssh连接信息
     * @param shell     shell语句
     * @return shell执行结果
     * @throws IOException 读写异常
     */
    @Override
    public StringBuilder exec(SSHEntity sshEntity, String shell) throws IOException {
        InputStream inputStream = null;
        StringBuilder result = new StringBuilder();
        try {
            // 认证登录信息
            if (this.login(sshEntity)) {
                // 登陆成功则打开一个会话
                Session session = connect.openSession();
                session.execCommand(shell);
                inputStream = session.getStdout();
                result = this.processStdout(inputStream);
                connect.close();
            }
        } finally {
            if (null != inputStream) {
                inputStream.close();
            }
        }
        return result;
    }

    private boolean login(SSHEntity sshEntity) {
        connect = new Connection(sshEntity.getHost(), sshEntity.getPort());
        try {
            connect.connect();
            return connect.authenticateWithPassword(sshEntity.getUser(), sshEntity.getPassword());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private StringBuilder processStdout(InputStream in) {
        byte[] buf = new byte[1024];
        StringBuilder builder = new StringBuilder();
        try {
            int length;
            while ((length = in.read(buf)) != -1) {
                builder.append(new String(buf, 0, length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder;
    }
}

